cmake_minimum_required(VERSION 3.18 FATAL_ERROR)

project(AppHost VERSION "1.0.0")
set(CMAKE_FOLDER "Application")

file(GLOB_RECURSE APP_SOURCES *.*)

# exclude main.cpp
list(FILTER APP_SOURCES EXCLUDE REGEX ".*main\\.cpp")
list(FILTER APP_SOURCES EXCLUDE REGEX ".*app\\.rc")

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "Sources" FILES ${APP_SOURCES})

# we need opengl
find_package(OpenGL COMPONENTS GLX EGL)
set(GL_ADDITION_DEF)
if (OpenGL_GLX_FOUND OR OpenGL_EGL_FOUND)
  if (OpenGL_GLX_FOUND)
    message(STATUS "Have OpenGL: INCLUDE ${OPENGL_INCLUDE_DIR} LIBRARY ${OPENGL_opengl_LIBRARY}")
    list(APPEND LINK_LIBS OpenGL::OpenGL)
  elseif(OpenGL_EGL_FOUND)
    message(STATUS "Have OpenGLES: INCLUDE ${OPENGL_EGL_INCLUDE_DIR} LIBRARY ${OPENGL_egl_LIBRARY}")
    list(APPEND LINK_LIBS GLESv2)
    list(APPEND GL_ADDITION_DEF -DIMGUI_IMPL_OPENGL_ES3)
  endif()
  set(HAVE_OPENGL 1)
elseif(WIN32)
  message(STATUS "Win32 Skip Find OpenGL")
  set(HAVE_OPENGL 1)
else()
  message(STATUS "No OpenGL found")
  set(HAVE_OPENGL 0)
endif()

# we need threads
find_package(Threads REQUIRED QUIET)

# 使用 sdl2 或者 glfw3(优先)
option(FORCE_USE_SDL2 "Force using SDL2 even if glfw3 was found" OFF)
pkg_check_modules(GLFW3 IMPORTED_TARGET glfw3)
if (GLFW3_FOUND AND NOT FORCE_USE_SDL2)
  message(STATUS "Backend using glfw3 v${GLFW3_VERSION}")
  set(BACKEND_DEFINITIONS -DUSING_GLFW=1 -DUSING_SDL=0)
  set(BACKEND_LINK PkgConfig::GLFW3)
else()
  pkg_check_modules(SDL2 IMPORTED_TARGET sdl2)
  if(SDL2_FOUND)
    message(STATUS "Backend using sdl2 ${SDL2_VERSION}")
    set(BACKEND_DEFINITIONS -DUSING_GLFW=0 -DUSING_SDL=1)
    set(BACKEND_LINK PkgConfig::SDL2)
  else()
    message(FATAL_ERROR "No GLFW3 or SDL2 found!!")
  endif()
endif()

set(EXTENSION_DEFS)
set(EXTENSION_LIBS)
if (UNIX)
  find_package(X11)
  if (X11_FOUND)
    list(APPEND EXTENSION_DEFS -DX11_OK=1)
    list(APPEND EXTENSION_LIBS X11::X11)
  else()
    list(APPEND EXTENSION_DEFS -DX11_OK=0)
  endif()
endif()

add_library(PiInfoApp-OBJS OBJECT ${APP_SOURCES})
target_link_libraries(PiInfoApp-OBJS PUBLIC pi_sdk ImGui glm ${BACKEND_LINK} cpr ${LINK_LIBS} ${CMAKE_DL_LIBS} Threads::Threads ${EXTENSION_LIBS})
target_include_directories(PiInfoApp-OBJS PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(PiInfoApp-OBJS PUBLIC ${GL_ADDITION_DEF} ${BACKEND_DEFINITIONS} ${EXTENSION_DEFS})
target_precompile_headers(PiInfoApp-OBJS PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/pch/app_pch.hpp)

add_executable(PiInfoApp main.cpp)
target_strip(PiInfoApp)
target_link_libraries(PiInfoApp PRIVATE PiInfoApp-OBJS)

if (MSVC)
  target_link_options(PiInfoApp PRIVATE $<IF:$<CONFIG:Debug>,/SUBSYSTEM:CONSOLE,/SUBSYSTEM:WINDOWS> /ENTRY:mainCRTStartup)
endif()

add_resource_folders(PiInfoApp ${CMAKE_CURRENT_SOURCE_DIR} assets)

if (WIN32)

  if (EXISTS ${CMAKE_CURRENT_BINARY_DIR}/build_number.txt)
    file(READ ${CMAKE_CURRENT_BINARY_DIR}/build_number.txt BUILD_NUMBER)
    math(EXPR BUILD_NUMBER "${BUILD_NUMBER}+1")
  else()
    set(BUILD_NUMBER 1)
  endif()

  set(ICON_PATH app_icon.ico)
  string(TIMESTAMP BUILD_DATE "%Y%m%d%H%M%S")

  set(INPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/app.rc.cmakein)
  set(OUTPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/app.rc)
  configure_file(${INPUT_FILE} ${OUTPUT_FILE})
  target_sources(PiInfoApp PRIVATE ${OUTPUT_FILE})

  add_custom_target(
    UpdateBuildNumber
    COMMAND ${CMAKE_COMMAND} -E echo "Current Build Number Is: ${BUILD_NUMBER}"
    COMMAND ${CMAKE_COMMAND} -E echo "${BUILD_NUMBER}" > "${CMAKE_CURRENT_BINARY_DIR}/build_number.txt"
    COMMENT "Update build number..."
  )
endif()

get_target_property(RESOURCE_ROOT_DIR Res RESOURCE_DIR)
message(STATUS "Resource Root Dir: ${RESOURCE_ROOT_DIR}")

set(V1 "${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
set(V2 "3.26.0")
message(STATUS "VERSION is ${V1} - ${V2}")

add_custom_target(
  CopyPiInfoResources ALL
  COMMAND ${CMAKE_COMMAND} -E $<IF:$<VERSION_LESS:${V1},${V2}>,copy_directory,copy_directory_if_different> ${RESOURCE_ROOT_DIR} $<TARGET_FILE_DIR:PiInfoApp>
  COMMENT "Copy Resources"
  DEPENDS PiInfoApp
)
